package ControlLayer;
import ModelLayer.*;

/**
 * Controller class for ReturnDVDCtr.
 * 
 * @author Tue Brodersen
 * @version 1.0 (2013-11-07)
 */
public class ReturnDVDCtr
{
    private static AddressCtr addrCtr;
    private static DVDCtr dvdCtr;
    private static ReturnDVDCtr instance;
    
    /**
     * Constructor for the ReturnDVD Controller
     */
    public ReturnDVDCtr()
    {
        addrCtr = AddressCtr.getInstance();
        dvdCtr = DVDCtr.getInstance();
    }
    
    /**
     * A method to get the ReturnDVD controller instance
     * 
     * @return Returns the instance of ReturnDVDCtr Controller of type object(ReturnDVDCtr)
     */
    public static ReturnDVDCtr getInstance()
    {
        if(instance == null)
        {
            instance = new ReturnDVDCtr();
        }
        
        return instance;
    }
    
        
    /**
     * A method to create a ReturnDVDCtr object and add a specimen
     * 
     * @param dvdId The ID of the DVD
     * @param specimenId The specimenId of the specimen
     * @param personId the ID of the person
     */
    public void returnDVD(long dvdId, long specimenId, long personId)
    {
        DvdSpecimen specimen = dvdCtr.findSpecimen(dvdId, specimenId);
        ReturnDVDCtr returnDvd = new ReturnDVDCtr();
        addrCtr.getPerson(personId).deleteBorrow(specimen);
        specimen.setAvailable(true);
    }
    
    /**
     * A method to see whether a specimen is available
     * 
     * @param dvdId The ID of the DVD
     * @param specimenId The ID of the specimen
     */
    public boolean isSpecimenAvailable(long dvdId, long specimenId)
    {
        DvdSpecimen specimen = dvdCtr.findSpecimen(dvdId, specimenId);
        return specimen.getAvailable();
    }
    
    /**
     * A method to list all rented specimens a person has
     * 
     * @param personId The ID of the person
     */
    public void listAllRentedCopies(long personId)
    {
        int index = 0;
        while(index < addrCtr.getPerson(personId).loans().size())
        {
            Borrow loan = addrCtr.getPerson(personId).getLoan(index);
            System.out.println("DVD ID: " + loan.getSpecimen().getDVDId() + " Copy ID: " + loan.getSpecimen().getDVDId() + " copy of: " + loan.getSpecimen().getDVDTitle());
            index++;
        }
    }
}
